package sii.challenge;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
import sii.challenge.dataset.Dataset;
import sii.challenge.predictors.MyPredictor;
import sii.challenge.predictors.Predictor;

/**
 * 
 * Classe principale che consente di avviare il sistema.
 * 
 * @author Marco Liceti
 *
 */
public final class Main {

	/**
	 * 
	 * Avvia il sistema.
	 * 
	 * @param args[0] il pathname del file contenente le coppie
	 * (user, item) da usare per il test
	 * 
	 * @param args[1] il pathname del file in cui scrivere l'output
	 * del sistema
	 * 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		Properties properties = new Properties();
		properties.load(new FileInputStream("challenge.properties"));
		Dataset dataset = Dataset.getInstance(properties);
		
		Predictor predictor = MyPredictor.getInstance(dataset);
		
		BufferedReader reader = new BufferedReader(new FileReader(args[0]));
		BufferedWriter writer = new BufferedWriter(new FileWriter(args[1]));
		
		int i = 1;
		String line = reader.readLine();
		while (line != null) {
			Scanner s = new Scanner(line);
			s.useDelimiter(",");
			int user = s.nextInt();
			int item = s.nextInt();
			
			int prediction = predictor.predictRating(user, item);
			
			writer.write(prediction + "\n");
			System.out.println(i + ": " + prediction);
			
			line = reader.readLine();
			i++;
		}
		
		reader.close();
		writer.close();
		
		dataset.close();
	}

}
